package controllers

import (
	"cashew.com/cashew_common/configs"
	"cashew.com/cashew_task/services"
	"github.com/beego/beego/v2/core/logs"
)

type AnchorController struct {
	BaseController
}

func (c *AnchorController) AnchorRating() {
	periodDays, _ := c.GetInt("period_days")

	ratingDate := c.GetString("rating_date")
	regionId, _ := c.GetInt("region_id")

	logs.Info("AnchorRating cron job start... ")

	if periodDays < 0 || periodDays > 400 {
		logs.Error("参数错误: period_days 必须大于0小于等于400")
		c.buildSystemErrorResult()
		return
	}

	c.Info("AnchorRatingParams: periodDays: ", periodDays, " ratingDate: ", ratingDate)

	workServerName := configs.WorkServerName
	if workServerName == "" {
		workServerName = "cashew" // 默认值
	}

	var anchorRatingFunc func(int, string, int) error
	switch workServerName {
	case "cashew":
		anchorRatingFunc = services.AnchorRating
	case "maoyin":
		anchorRatingFunc = services.AnchorMaoyinRating
	default:
		logs.Error("未知的 workServerName: ", workServerName)
		c.buildSystemErrorResult()
		return
	}

	var err error
	// 调用对应的服务方法
	err = anchorRatingFunc(periodDays, ratingDate, regionId)
	if err != nil {
		// 记录详细错误日志
		c.Error("Failed to calculate anchor rating: ", err)
		c.buildSystemErrorResult()
		return
	}

	logs.Info("AnchorRating cron job completed.")
	c.initResult()
	return
}

/**
 * 主播数据每日统计, 至少在北京时间每天凌晨1点后执行一次
 * @api {get} /cron/anchorWorkStatsDay
 */
func (c *AnchorController) AnchorWorkStatsDay() {

	err := services.AnchorWorkStatsDay()

	if err != nil {
		c.Error("统计失败,time=%s,err=&s", err.Error())
		c.buildSystemErrorResult()
		return
	}

	c.Error("统计成功")
	c.initResult()
	return

}
